package com.hirsi.common.core.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.stereotype.Component;

/**
 * 开发者租户
 */
@Component
public class DeveloperCodeTenantLineHandler implements TenantLineHandler {

    //忽略的表
    private final String[] IGNORE_TABLES ={};

    //租户线程变量
    private ThreadLocal<String> developerCodeLocal = new ThreadLocal<>();

    public void setTenantId(String developerCode) {
        this.developerCodeLocal.set(developerCode);
    }

    @Override
    public Expression getTenantId() {
        return new StringValue(developerCodeLocal.get());
    }

    @Override
    public String getTenantIdColumn() {
        return "developer_code";
    }

    @Override
    public boolean ignoreTable(String tableName) {
        return CollUtil.newArrayList(IGNORE_TABLES).contains(tableName) || StrUtil.isBlank(developerCodeLocal.get());
    }
}
